<template>
  <div class="edit-dialog">
    <el-dialog
      v-model="dialog"
      :title="titleName"
      :visible.sync="dialog"
      :close-on-click-modal="false"
      :show-close="false"
      width="700px"
      center
    >
      <el-form :model="addFormData" :rules="rules" ref="formRef" label-width="auto">
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <div class="grid-content bg-purple">参数</div>
          </el-col>
          <el-col :span="6" class="col">
            <div class="grid-content bg-purple">公式</div>
          </el-col>
          <el-col :span="6" class="col">
            <div class="grid-content bg-purple">a</div>
          </el-col>
          <el-col :span="6" class="col">
            <div class="grid-content bg-purple">b</div>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="A相电压(V)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.voltage_a_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.voltage_a_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="B相电压(V)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.voltage_b_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.voltage_b_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="C相电压(V)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.voltage_c_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.voltage_c_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="A相电流(A)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ampere_a_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ampere_a_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="B相电流(A)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ampere_b_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ampere_b_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="C相电流(A)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ampere_c_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ampere_c_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="A相有功功率(kW)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_a_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_a_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="B相有功功率(kW)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_b_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_b_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="C相有功功率(kW)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_c_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_c_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="总有功功率(kW)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.power_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="A相能耗(kW·h)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_a_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_a_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="B相能耗(kW·h)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label="y=ax+b"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_b_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_b_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="C相能耗(kW·h)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_c_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_c_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="6" class="col">
            <el-form-item label="总能耗(kW·h)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.watt_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showNoPower">
          <el-col :span="6" class="col">
            <el-form-item label="A相无功功率(kVar)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.qa_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.qa_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showNoPower">
          <el-col :span="6" class="col">
            <el-form-item label="B相无功功率(kVar)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.qb_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.qb_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showNoPower">
          <el-col :span="6" class="col">
            <el-form-item label="C相无功功率(kVar)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.qc_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.qc_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showNoPower">
          <el-col :span="6" class="col">
            <el-form-item label="总无功功率(kVar)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.q_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.q_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showPowerFactor">
          <el-col :span="6" class="col">
            <el-form-item label="A相功率因数" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ca_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ca_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showPowerFactor">
          <el-col :span="6" class="col">
            <el-form-item label="B相功率因数" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.cb_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.cb_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showPowerFactor">
          <el-col :span="6" class="col">
            <el-form-item label="C相功率因数" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.cc_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.cc_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showPowerFactor">
          <el-col :span="6" class="col">
            <el-form-item label="总功率因数" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.c_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.c_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row :gutter="5" v-if="showCarbonEmissions">
          <el-col :span="6" class="col">
            <el-form-item label="碳排放量(kgCO₂)" class="grid-content"></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-form-item label=""></el-form-item>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ce_a" :precision="1" :step="0.1" :min="0" :max="10"
                             class="input"></el-input-number>
          </el-col>
          <el-col :span="6" class="col">
            <el-input-number v-model="addFormData.ce_b" :precision="1" :step="1" :min="-1024" :max="1024"
                             class="input"></el-input-number>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="grid-content bg-purple-light">注：参数a默认1，参数b默认为0</div>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" :loading="submitLoading" icon="el-icon-check"
                     @click="submit('formRef')">确 认</el-button>
          <el-button icon="el-icon-close" @click="handleClose">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {deviceAdjustInfo, deviceAdjust} from '@/api/deviceManage'
  export default {
    name: "adjustForm",
    props: {
      formData: Object,
      showNoPower: Boolean,
      showPowerFactor: Boolean,
      showCarbonEmissions: Boolean,
      showDialog: Boolean
    },
    data() {
      return {
        addFormData: {
          deviceId: "",
          voltage_a_a: 1,
          voltage_a_b: 0,
          voltage_b_a: 1,
          voltage_b_b: 0,
          voltage_c_a: 1,
          voltage_c_b: 0,
          ampere_a_a: 1,
          ampere_a_b: 0,
          ampere_b_a: 1,
          ampere_b_b: 0,
          ampere_c_a: 1,
          ampere_c_b: 0,
          power_a_a: 1,
          power_a_b: 0,
          power_b_a: 1,
          power_b_b: 0,
          power_c_a: 1,
          power_c_b: 0,
          power_a: 1,
          power_b: 0,
          watt_a_a: 1,
          watt_a_b: 0,
          watt_b_a: 1,
          watt_b_b: 0,
          watt_c_a: 1,
          watt_c_b: 0,
          watt_a: 1,
          watt_b: 0,
          qa_a: 1,
          qa_b: 0,
          qb_a: 1,
          qb_b: 0,
          qc_a: 1,
          qc_b: 0,
          q_a: 1,
          q_b: 0,
          ca_a: 1,
          ca_b: 0,
          cb_a: 1,
          cb_b: 0,
          cc_a: 1,
          cc_b: 0,
          c_a: 1,
          c_b: 0,
          ce_a: 1,
          ce_b: 0
        },
        titleName: "设备参数校准设备",
        submitLoading: false,
        rules: {
          voltage_a_a: [{required: true, message: "请输入0到10之间的数字", trigger: "blur"}],
          voltage_a_b: [{required: true, message: "请输入-1024到1024之间的数字", trigger: "blur"}]
        }
      };
    },
    mounted() {
      this.addFormData.deviceId = this.$route.query.deviceId;
      //console.log("deviceId=" + this.addFormData.deviceId);
      deviceAdjustInfo(this.addFormData.deviceId).then(response => {
        //console.log("接收到的数据:", response .value);
        if (response && response.value) {
          this.parseDataToForm(response.value);
        }
      });
    },
    computed: {
      dialog: function () {
        return this.showDialog;
      },
    },
    watch: {
      // 当 showDialog 改变为 true 时，加载数据
      async showDialog(newVal) {
        if (newVal) {
          // 如果 deviceId 还没设置，可以从 props 或其他方式获取
          if (!this.addFormData.deviceId) {
            this.addFormData.deviceId = this.$route.query.deviceId;
          }
          await this.loadData(); // 调用加载数据的方法
        }
      }
    },
    methods: {
      // 取消事件
      handleClose() {
        this.$emit("adjustVisible", false);
      },
      // 解析 value 数组并赋值给表单
      parseDataToForm(dataArray) {
        dataArray.forEach(item => {
          const typeMap = {
            VOLTAGE_A: 'voltage_a',
            VOLTAGE_B: 'voltage_b',
            VOLTAGE_C: 'voltage_c',
            AMPERE_A: 'ampere_a',
            AMPERE_B: 'ampere_b',
            AMPERE_C: 'ampere_c',
            POWER_A: 'power_a',
            POWER_B: 'power_b',
            POWER_C: 'power_c',
            POWER: 'power',
            WATT_A: 'watt_a',
            WATT_B: 'watt_b',
            WATT_C: 'watt_c',
            WATT: 'watt',
            QA: 'qa',
            QB: 'qb',
            QC: 'qc',
            Q: 'q',
            CA: 'ca',
            CB: 'cb',
            CC: 'cc',
            C: 'c',
            CE: 'ce'
          };
          const fieldName = typeMap[item.type];
          if (fieldName) {
            this.addFormData[`${fieldName}_a`] = item.a;
            this.addFormData[`${fieldName}_b`] = item.b;
          }
        });
      },
      async loadData() {
        try {
          const response = await deviceAdjustInfo(this.addFormData.deviceId);
          if (response && response.value) {
            this.parseDataToForm(response.value);
          }
        } catch (error) {
          console.error('加载设备校准信息失败:', error);
        }
      },
      submit(formName) {
        this.submitLoading = true;
        this.$refs[formName].validate(valid => {
          if (valid) {
            const that = this;
            // 创建要发送的数据对象
            let sendData = {
              deviceId: this.addFormData.deviceId,
              factors: []
            };

            // 定义类型映射
            const typeMap = [
              {key: 'voltage_a', type: 'VOLTAGE_A'},
              {key: 'voltage_b', type: 'VOLTAGE_B'},
              {key: 'voltage_c', type: 'VOLTAGE_C'},
              {key: 'ampere_a', type: 'AMPERE_A'},
              {key: 'ampere_b', type: 'AMPERE_B'},
              {key: 'ampere_c', type: 'AMPERE_C'},
              {key: 'power_a', type: 'POWER_A'},
              {key: 'power_b', type: 'POWER_B'},
              {key: 'power_c', type: 'POWER_C'},
              {key: 'power', type: 'POWER'},
              {key: 'watt_a', type: 'WATT_A'},
              {key: 'watt_b', type: 'WATT_B'},
              {key: 'watt_c', type: 'WATT_C'},
              {key: 'watt', type: 'WATT'},
              {key: 'qa', type: 'QA'},
              {key: 'qb', type: 'QB'},
              {key: 'qc', type: 'QC'},
              {key: 'q', type: 'Q'},
              {key: 'ca', type: 'CA'},
              {key: 'cb', type: 'CB'},
              {key: 'cc', type: 'CC'},
              {key: 'c', type: 'C'},
              {key: 'ce', type: 'CE'}
            ];

            // 填充factors数组
            typeMap.forEach(item => {
              sendData.factors.push({
                type: item.type,
                a: this.addFormData[`${item.key}_a`],
                b: this.addFormData[`${item.key}_b`]
              });
            });

            //console.log('准备发送的数据:', JSON.stringify(sendData));

            deviceAdjust(sendData)
              .then(res => {
                that.submitLoading = false;
                if (res['code'] == 200) {
                  that.$emit("adjustVisible", false);
                  that.$emit("getDeviceData");
                  that.$message.success('校准成功!')
                } else {
                  that.$message(res['msg'])
                }
              })
              .catch(() => {
                that.submitLoading = false
              })
          } else {
            this.submitLoading = false;
            return false;
          }
        });
      }
    },
  };
</script>

<style>
  .col {
    border-radius: 4px;
    height: 38px;
  }

  .input {
    width: auto; /* 让输入框宽度占满父容器 */
    box-sizing: border-box; /* 确保 padding 和 border 不会增加额外的宽度 */
  }

  .bg-purple-light {
    height:36px;
    text-align: center;
    line-height: 36px;
    color: #ff8b5b;
    background: #d3dce6;
  }

  .bg-purple {
    height:36px;
    text-align: center;
    line-height: 36px;
    font-weight: bold;
    background: #d3dce6;
  }

  .grid-content {
    border-radius: 4px;
    min-height: 30px;
  }
</style>
